import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
    {
        path: "/",
        redirect: "/main/found/selected",
    },
    {
        path: "/main",
        name: "main",
        // redirect: "/found/selected",
        component: () => import("@/views/main/main.vue"),
        children: [
            {
                path: "bookshelf",
                name: "bookshelf",
                component: () => import("@/views/bookshelf/bookshelf.vue"),
            },
            {
                path: "found",
                name: "found",
                redirect: "/main/found/selected",
                component: () => import("@/views/found/found.vue"),
                children: [
                    {
                        path: "selected",
                        name: "selected",
                        component: () => import("@/views/found/children/selected.vue"),
                    },
                    {
                        path: "gratis",
                        name: "gratis",
                        component: () => import("@/views/found/children/gratis.vue"),
                    },
                    {
                        path: "schoolboy",
                        name: "schoolboy",
                        component: () => import("@/views/found/children/schoolboy.vue"),
                    },
                    {
                        path: "schoolgirl",
                        name: "schoolgirl",
                        component: () => import("@/views/found/children/schoolgirl.vue"),
                    },
                    {
                        path: "publication",
                        name: "publication",
                        component: () => import("@/views/found/children/publication.vue"),
                    },
                    {
                        path: "listenbook",
                        name: "listenbook",
                        component: () => import("@/views/found/children/listenbook.vue"),
                    },
                ],
            },
            {
                path: "my",
                name: "my",
                component: () => import("@/views/my/my.vue"),
            },
            {
                path: "classify",
                name: "classify",
                component: () => import("@/views/classify/classify.vue"),
            },
        ],
    },
    {
        path: "/classify",
        name: "classify",
        redirect: "/classify/boys",
        component: () => import("@/views/classify/classify.vue"),
        children: [
            {
                path: "boys",
                name: "boys",
                component: () => import("@/views/classify/children/boys.vue"),
            },
            {
                path: "girls",
                name: "girls",
                component: () => import("@/views/classify/children/girls.vue"),
            },
        ],
    },
    {
        path: "/categorydetails",
        name: "categorydetails",
        component: () => import("@/views/classify/category-details.vue"),
    },
    {
        path: "/detail",
        name: "detail",
        component: () => import("@/views/detail/detail.vue"),
        // props: (route) => route.query,
    },
    {
        path: "/detaillist",
        name: "detaillist",
        component: () => import("@/views/detail/detail-list.vue"),
    },
    {
        path: "/novel",
        name: "novel",
        component: () => import("@/views/detail/novel-content.vue"),
    },
    {
        path: "/search",
        name: "search",
        redirect: "/search/hot",
        component: () => import("@/views/search/search.vue"),
        children: [
            {
                path: "hot",
                name: "hot",
                component: () => import("@/views/search/hotsearch.vue"),
            },
            {
                path: "searchlist",
                name: "searchlist",
                component: () => import("@/views/search/searchlist.vue"),
            },
        ],
    },
    {
        path: "/login",
        name: "login",
        component: () => import("@/views/login/login.vue"),
    },
    {
        path: "/register",
        name: "register",
        component: () => import("@/views/register/register.vue"),
    },
    {
        path: "/ranking",
        name: "ranking",
        component: () => import("@/views/ranking/ranking.vue"),
    },
];

const router = new VueRouter({
    routes,
});

router.beforeEach((to, from, next) => {
    if (to.name != "login") {
        if (to.name == "selected" || to.name == "register") {
            next();
        } else {
            if (localStorage.token) {
                next();
            } else {
                next({ name: "login" });
            }
        }
    } else {
        // 直接放行
        next();
    }
});

export default router;
